home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / PEAR / Command / Package.php < prev    next >
PHP Script  |  2004-10-01  |  25KB  |  719 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@php.net>                                   |
  17. // |          Martin Jansen <mj@php.net>                                  |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Package.php,v 1.60 2004/01/31 22:39:25 cellog Exp $
  21.  
  22. require_once 'PEAR/Common.php';
  23. require_once 'PEAR/Command/Common.php';
  24.  
  25. class PEAR_Command_Package extends PEAR_Command_Common
  26. {
  27.     // {{{ properties
  28.  
  29.     var $commands = array(
  30.         'package' => array(
  31.             'summary' => 'Build Package',
  32.             'function' => 'doPackage',
  33.             'shortcut' => 'p',
  34.             'options' => array(
  35.                 'nocompress' => array(
  36.                     'shortopt' => 'Z',
  37.                     'doc' => 'Do not gzip the package file'
  38.                     ),
  39.                 'showname' => array(
  40.                     'shortopt' => 'n',
  41.                     'doc' => 'Print the name of the packaged file.',
  42.                     ),
  43.                 ),
  44.             'doc' => '[descfile]
  45. Creates a PEAR package from its description file (usually called
  46. package.xml).
  47. '
  48.             ),
  49.         'package-validate' => array(
  50.             'summary' => 'Validate Package Consistency',
  51.             'function' => 'doPackageValidate',
  52.             'shortcut' => 'pv',
  53.             'options' => array(),
  54.             'doc' => '
  55. ',
  56.             ),
  57.         'cvsdiff' => array(
  58.             'summary' => 'Run a "cvs diff" for all files in a package',
  59.             'function' => 'doCvsDiff',
  60.             'shortcut' => 'cd',
  61.             'options' => array(
  62.                 'quiet' => array(
  63.                     'shortopt' => 'q',
  64.                     'doc' => 'Be quiet',
  65.                     ),
  66.                 'reallyquiet' => array(
  67.                     'shortopt' => 'Q',
  68.                     'doc' => 'Be really quiet',
  69.                     ),
  70.                 'date' => array(
  71.                     'shortopt' => 'D',
  72.                     'doc' => 'Diff against revision of DATE',
  73.                     'arg' => 'DATE',
  74.                     ),
  75.                 'release' => array(
  76.                     'shortopt' => 'R',
  77.                     'doc' => 'Diff against tag for package release REL',
  78.                     'arg' => 'REL',
  79.                     ),
  80.                 'revision' => array(
  81.                     'shortopt' => 'r',
  82.                     'doc' => 'Diff against revision REV',
  83.                     'arg' => 'REV',
  84.                     ),
  85.                 'context' => array(
  86.                     'shortopt' => 'c',
  87.                     'doc' => 'Generate context diff',
  88.                     ),
  89.                 'unified' => array(
  90.                     'shortopt' => 'u',
  91.                     'doc' => 'Generate unified diff',
  92.                     ),
  93.                 'ignore-case' => array(
  94.                     'shortopt' => 'i',
  95.                     'doc' => 'Ignore case, consider upper- and lower-case letters equivalent',
  96.                     ),
  97.                 'ignore-whitespace' => array(
  98.                     'shortopt' => 'b',
  99.                     'doc' => 'Ignore changes in amount of white space',
  100.                     ),
  101.                 'ignore-blank-lines' => array(
  102.                     'shortopt' => 'B',
  103.                     'doc' => 'Ignore changes that insert or delete blank lines',
  104.                     ),
  105.                 'brief' => array(
  106.                     'doc' => 'Report only whether the files differ, no details',
  107.                     ),
  108.                 'dry-run' => array(
  109.                     'shortopt' => 'n',
  110.                     'doc' => 'Don\'t do anything, just pretend',
  111.                     ),
  112.                 ),
  113.             'doc' => '<package.xml>
  114. Compares all the files in a package.  Without any options, this
  115. command will compare the current code with the last checked-in code.
  116. Using the -r or -R option you may compare the current code with that
  117. of a specific release.
  118. ',
  119.             ),
  120.         'cvstag' => array(
  121.             'summary' => 'Set CVS Release Tag',
  122.             'function' => 'doCvsTag',
  123.             'shortcut' => 'ct',
  124.             'options' => array(
  125.                 'quiet' => array(
  126.                     'shortopt' => 'q',
  127.                     'doc' => 'Be quiet',
  128.                     ),
  129.                 'reallyquiet' => array(
  130.                     'shortopt' => 'Q',
  131.                     'doc' => 'Be really quiet',
  132.                     ),
  133.                 'slide' => array(
  134.                     'shortopt' => 'F',
  135.                     'doc' => 'Move (slide) tag if it exists',
  136.                     ),
  137.                 'delete' => array(
  138.                     'shortopt' => 'd',
  139.                     'doc' => 'Remove tag',
  140.                     ),
  141.                 'dry-run' => array(
  142.                     'shortopt' => 'n',
  143.                     'doc' => 'Don\'t do anything, just pretend',
  144.                     ),
  145.                 ),
  146.             'doc' => '<package.xml>
  147. Sets a CVS tag on all files in a package.  Use this command after you have
  148. packaged a distribution tarball with the "package" command to tag what
  149. revisions of what files were in that release.  If need to fix something
  150. after running cvstag once, but before the tarball is released to the public,
  151. use the "slide" option to move the release tag.
  152. ',
  153.             ),
  154.         'run-tests' => array(
  155.             'summary' => 'Run Regression Tests',
  156.             'function' => 'doRunTests',
  157.             'shortcut' => 'rt',
  158.             'options' => array(),
  159.             'doc' => '[testfile|dir ...]
  160. Run regression tests with PHP\'s regression testing script (run-tests.php).',
  161.             ),
  162.         'package-dependencies' => array(
  163.             'summary' => 'Show package dependencies',
  164.             'function' => 'doPackageDependencies',
  165.             'shortcut' => 'pd',
  166.             'options' => array(),
  167.             'doc' => '
  168. List all depencies the package has.'
  169.             ),
  170.         'sign' => array(
  171.             'summary' => 'Sign a package distribution file',
  172.             'function' => 'doSign',
  173.             'shortcut' => 'si',
  174.             'options' => array(),
  175.             'doc' => '<package-file>
  176. Signs a package distribution (.tar or .tgz) file with GnuPG.',
  177.             ),
  178.         'makerpm' => array(
  179.             'summary' => 'Builds an RPM spec file from a PEAR package',
  180.             'function' => 'doMakeRPM',
  181.             'shortcut' => 'rpm',
  182.             'options' => array(
  183.                 'spec-template' => array(
  184.                     'shortopt' => 't',
  185.                     'arg' => 'FILE',
  186.                     'doc' => 'Use FILE as RPM spec file template'
  187.                     ),
  188.                 'rpm-pkgname' => array(
  189.                     'shortopt' => 'p',
  190.                     'arg' => 'FORMAT',
  191.                     'doc' => 'Use FORMAT as format string for RPM package name, %s is replaced
  192. by the PEAR package name, defaults to "PEAR::%s".',
  193.                     ),
  194.                 ),
  195.             'doc' => '<package-file>
  196.  
  197. Creates an RPM .spec file for wrapping a PEAR package inside an RPM
  198. package.  Intended to be used from the SPECS directory, with the PEAR
  199. package tarball in the SOURCES directory:
  200.  
  201. $ pear makerpm ../SOURCES/Net_Socket-1.0.tgz
  202. Wrote RPM spec file PEAR::Net_Geo-1.0.spec
  203. $ rpm -bb PEAR::Net_Socket-1.0.spec
  204. ...
  205. Wrote: /usr/src/redhat/RPMS/i386/PEAR::Net_Socket-1.0-1.i386.rpm
  206. ',
  207.             ),
  208.         );
  209.  
  210.     var $output;
  211.  
  212.     // }}}
  213.     // {{{ constructor
  214.  
  215.     /**
  216.      * PEAR_Command_Package constructor.
  217.      *
  218.      * @access public
  219.      */
  220.     function PEAR_Command_Package(&$ui, &$config)
  221.     {
  222.         parent::PEAR_Command_Common($ui, $config);
  223.     }
  224.  
  225.     // }}}
  226.  
  227.     // {{{ _displayValidationResults()
  228.  
  229.     function _displayValidationResults($err, $warn, $strict = false)
  230.     {
  231.         foreach ($err as $e) {
  232.             $this->output .= "Error: $e\n";
  233.         }
  234.         foreach ($warn as $w) {
  235.             $this->output .= "Warning: $w\n";
  236.         }
  237.         $this->output .= sprintf('Validation: %d error(s), %d warning(s)'."\n",
  238.                                        sizeof($err), sizeof($warn));
  239.         if ($strict && sizeof($err) > 0) {
  240.             $this->output .= "Fix these errors and try again.";
  241.             return false;
  242.         }
  243.         return true;
  244.     }
  245.  
  246.     // }}}
  247.     // {{{ doPackage()
  248.  
  249.     function doPackage($command, $options, $params)
  250.     {
  251.         $this->output = '';
  252.         include_once 'PEAR/Packager.php';
  253.         $pkginfofile = isset($params[0]) ? $params[0] : 'package.xml';
  254.         $packager =& new PEAR_Packager();
  255.         $err = $warn = array();
  256.         $dir = dirname($pkginfofile);
  257.         $compress = empty($options['nocompress']) ? true : false;
  258.         $result = $packager->package($pkginfofile, $compress);
  259.         if (PEAR::isError($result)) {
  260.             $this->ui->outputData($this->output, $command);
  261.             return $this->raiseError($result);
  262.         }
  263.         // Don't want output, only the package file name just created
  264.         if (isset($options['showname'])) {
  265.             $this->output = $result;
  266.         }
  267.         /* (cox) What is supposed to do that code?
  268.         $lines = explode("\n", $this->output);
  269.         foreach ($lines as $line) {
  270.             $this->output .= $line."n";
  271.         }
  272.         */
  273.         if (PEAR::isError($result)) {
  274.             $this->output .= "Package failed: ".$result->getMessage();
  275.         }
  276.         $this->ui->outputData($this->output, $command);
  277.         return true;
  278.     }
  279.  
  280.     // }}}
  281.     // {{{ doPackageValidate()
  282.  
  283.     function doPackageValidate($command, $options, $params)
  284.     {
  285.         $this->output = '';
  286.         if (sizeof($params) < 1) {
  287.             $params[0] = "package.xml";
  288.         }
  289.         $obj = new PEAR_Common;
  290.         $info = null;
  291.         if ($fp = @fopen($params[0], "r")) {
  292.             $test = fread($fp, 5);
  293.             fclose($fp);
  294.             if ($test == "<?xml") {
  295.                 $info = $obj->infoFromDescriptionFile($params[0]);
  296.             }
  297.         }
  298.         if (empty($info)) {
  299.             $info = $obj->infoFromTgzFile($params[0]);
  300.         }
  301.         if (PEAR::isError($info)) {
  302.             return $this->raiseError($info);
  303.         }
  304.         $obj->validatePackageInfo($info, $err, $warn);
  305.         $this->_displayValidationResults($err, $warn);
  306.         $this->ui->outputData($this->output, $command);
  307.         return true;
  308.     }
  309.  
  310.     // }}}
  311.     // {{{ doCvsTag()
  312.  
  313.     function doCvsTag($command, $options, $params)
  314.     {
  315.         $this->output = '';
  316.         $_cmd = $command;
  317.         if (sizeof($params) < 1) {
  318.             $help = $this->getHelp($command);
  319.             return $this->raiseError("$command: missing parameter: $help[0]");
  320.         }
  321.         $obj = new PEAR_Common;
  322.         $info = $obj->infoFromDescriptionFile($params[0]);
  323.         if (PEAR::isError($info)) {
  324.             return $this->raiseError($info);
  325.         }
  326.         $err = $warn = array();
  327.         $obj->validatePackageInfo($info, $err, $warn);
  328.         if (!$this->_displayValidationResults($err, $warn, true)) {
  329.             $this->ui->outputData($this->output, $command);
  330.             break;
  331.         }
  332.         $version = $info['version'];
  333.         $cvsversion = preg_replace('/[^a-z0-9]/i', '_', $version);
  334.         $cvstag = "RELEASE_$cvsversion";
  335.         $files = array_keys($info['filelist']);
  336.         $command = "cvs";
  337.         if (isset($options['quiet'])) {
  338.             $command .= ' -q';
  339.         }
  340.         if (isset($options['reallyquiet'])) {
  341.             $command .= ' -Q';
  342.         }
  343.         $command .= ' tag';
  344.         if (isset($options['slide'])) {
  345.             $command .= ' -F';
  346.         }
  347.         if (isset($options['delete'])) {
  348.             $command .= ' -d';
  349.         }
  350.         $command .= ' ' . $cvstag . ' ' . escapeshellarg($params[0]);
  351.         foreach ($files as $file) {
  352.             $command .= ' ' . escapeshellarg($file);
  353.         }
  354.         if ($this->config->get('verbose') > 1) {
  355.             $this->output .= "+ $command\n";
  356.         }
  357.         $this->output .= "+ $command\n";
  358.         if (empty($options['dry-run'])) {
  359.             $fp = popen($command, "r");
  360.             while ($line = fgets($fp, 1024)) {
  361.                 $this->output .= rtrim($line)."\n";
  362.             }
  363.             pclose($fp);
  364.         }
  365.         $this->ui->outputData($this->output, $_cmd);
  366.         return true;
  367.     }
  368.  
  369.     // }}}
  370.     // {{{ doCvsDiff()
  371.  
  372.     function doCvsDiff($command, $options, $params)
  373.     {
  374.         $this->output = '';
  375.         if (sizeof($params) < 1) {
  376.             $help = $this->getHelp($command);
  377.             return $this->raiseError("$command: missing parameter: $help[0]");
  378.         }
  379.         $obj = new PEAR_Common;
  380.         $info = $obj->infoFromDescriptionFile($params[0]);
  381.         if (PEAR::isError($info)) {
  382.             return $this->raiseError($info);
  383.         }
  384.         $files = array_keys($info['filelist']);
  385.         $cmd = "cvs";
  386.         if (isset($options['quiet'])) {
  387.             $cmd .= ' -q';
  388.             unset($options['quiet']);
  389.         }
  390.         if (isset($options['reallyquiet'])) {
  391.             $cmd .= ' -Q';
  392.             unset($options['reallyquiet']);
  393.         }
  394.         if (isset($options['release'])) {
  395.             $cvsversion = preg_replace('/[^a-z0-9]/i', '_', $options['release']);
  396.             $cvstag = "RELEASE_$cvsversion";
  397.             $options['revision'] = $cvstag;
  398.             unset($options['release']);
  399.         }
  400.         $execute = true;
  401.         if (isset($options['dry-run'])) {
  402.             $execute = false;
  403.             unset($options['dry-run']);
  404.         }
  405.         $cmd .= ' diff';
  406.         // the rest of the options are passed right on to "cvs diff"
  407.         foreach ($options as $option => $optarg) {
  408.             $arg = @$this->commands[$command]['options'][$option]['arg'];
  409.             $short = @$this->commands[$command]['options'][$option]['shortopt'];
  410.             $cmd .= $short ? " -$short" : " --$option";
  411.             if ($arg && $optarg) {
  412.                 $cmd .= ($short ? '' : '=') . escapeshellarg($optarg);
  413.             }
  414.         }
  415.         foreach ($files as $file) {
  416.             $cmd .= ' ' . escapeshellarg($file);
  417.         }
  418.         if ($this->config->get('verbose') > 1) {
  419.             $this->output .= "+ $cmd\n";
  420.         }
  421.         if ($execute) {
  422.             $fp = popen($cmd, "r");
  423.             while ($line = fgets($fp, 1024)) {
  424.                 $this->output .= rtrim($line)."\n";
  425.             }
  426.             pclose($fp);
  427.         }
  428.         $this->ui->outputData($this->output, $command);
  429.         return true;
  430.     }
  431.  
  432.     // }}}
  433.     // {{{ doRunTests()
  434.  
  435.     function doRunTests($command, $options, $params)
  436.     {
  437.         $cwd = getcwd();
  438.         $php = $this->config->get('php_bin');
  439.         putenv("TEST_PHP_EXECUTABLE=$php");
  440.         // all core PEAR tests use this constant to determine whether they should be run or not
  441.         putenv("PHP_PEAR_RUNTESTS=1");
  442.         $ip = ini_get("include_path");
  443.         $ps = OS_WINDOWS ? ';' : ':';
  444.         $run_tests = $rtsts = $this->config->get('php_dir') . DIRECTORY_SEPARATOR . 'run-tests.php';
  445.         if (!file_exists($run_tests)) {
  446.             $run_tests = PEAR_INSTALL_DIR . DIRECTORY_SEPARATOR . 'run-tests.php';
  447.             if (!file_exists($run_tests)) {
  448.                 return $this->raiseError("No run-tests.php file found. Please copy this ".
  449.                                                 "file from the sources of your PHP distribution to $rtsts");
  450.             }
  451.         }
  452.         if (OS_WINDOWS) {
  453.             // note, this requires a slightly modified version of run-tests.php
  454.             // for some setups
  455.             // unofficial download location is in the pear-dev archives
  456.             $argv = $params;
  457.             array_unshift($argv, $run_tests);
  458.             $argc = count($argv);
  459.             include $run_tests;
  460.         } else {
  461.             $plist = implode(' ', $params);
  462.             $cmd = "$php -d include_path=$cwd$ps$ip -f $run_tests -- $plist";
  463.             system($cmd);
  464.         }
  465.         return true;
  466.     }
  467.  
  468.     // }}}
  469.     // {{{ doPackageDependencies()
  470.  
  471.     function doPackageDependencies($command, $options, $params)
  472.     {
  473.         // $params[0] -> the PEAR package to list its information
  474.         if (sizeof($params) != 1) {
  475.             return $this->raiseError("bad parameter(s), try \"help $command\"");
  476.         }
  477.  
  478.         $obj = new PEAR_Common();
  479.         if (PEAR::isError($info = $obj->infoFromAny($params[0]))) {
  480.             return $this->raiseError($info);
  481.         }
  482.  
  483.         if (is_array($info['release_deps'])) {
  484.             $data = array(
  485.                 'caption' => 'Dependencies for ' . $info['package'],
  486.                 'border' => true,
  487.                 'headline' => array("Type", "Name", "Relation", "Version"),
  488.                 );
  489.  
  490.             foreach ($info['release_deps'] as $d) {
  491.  
  492.                 if (isset($this->_deps_rel_trans[$d['rel']])) {
  493.                     $rel = $this->_deps_rel_trans[$d['rel']];
  494.                 } else {
  495.                     $rel = $d['rel'];
  496.                 }
  497.  
  498.                 if (isset($this->_deps_type_trans[$d['type']])) {
  499.                     $type = ucfirst($this->_deps_type_trans[$d['type']]);
  500.                 } else {
  501.                     $type = $d['type'];
  502.                 }
  503.  
  504.                 if (isset($d['name'])) {
  505.                     $name = $d['name'];
  506.                 } else {
  507.                     $name = '';
  508.                 }
  509.  
  510.                 if (isset($d['version'])) {
  511.                     $version = $d['version'];
  512.                 } else {
  513.                     $version = '';
  514.                 }
  515.  
  516.                 $data['data'][] = array($type, $name, $rel, $version);
  517.             }
  518.  
  519.             $this->ui->outputData($data, $command);
  520.             return true;
  521.         }
  522.  
  523.         // Fallback
  524.         $this->ui->outputData("This package does not have any dependencies.", $command);
  525.     }
  526.  
  527.     // }}}
  528.     // {{{ doSign()
  529.  
  530.     function doSign($command, $options, $params)
  531.     {
  532.         // should move most of this code into PEAR_Packager
  533.         // so it'll be easy to implement "pear package --sign"
  534.         if (sizeof($params) != 1) {
  535.             return $this->raiseError("bad parameter(s), try \"help $command\"");
  536.         }
  537.         if (!file_exists($params[0])) {
  538.             return $this->raiseError("file does not exist: $params[0]");
  539.         }
  540.         $obj = new PEAR_Common;
  541.         $info = $obj->infoFromTgzFile($params[0]);
  542.         if (PEAR::isError($info)) {
  543.             return $this->raiseError($info);
  544.         }
  545.         include_once "Archive/Tar.php";
  546.         include_once "System.php";
  547.         $tar = new Archive_Tar($params[0]);
  548.         $tmpdir = System::mktemp('-d pearsign');
  549.         if (!$tar->extractList('package.xml package.sig', $tmpdir)) {
  550.             return $this->raiseError("failed to extract tar file");
  551.         }
  552.         if (file_exists("$tmpdir/package.sig")) {
  553.             return $this->raiseError("package already signed");
  554.         }
  555.         @unlink("$tmpdir/package.sig");
  556.         $input = $this->ui->userDialog($command,
  557.                                        array('GnuPG Passphrase'),
  558.                                        array('password'));
  559.         $gpg = popen("gpg --batch --passphrase-fd 0 --armor --detach-sign --output $tmpdir/package.sig $tmpdir/package.xml 2>/dev/null", "w");
  560.         if (!$gpg) {
  561.             return $this->raiseError("gpg command failed");
  562.         }
  563.         fwrite($gpg, "$input[0]\r");
  564.         if (pclose($gpg) || !file_exists("$tmpdir/package.sig")) {
  565.             return $this->raiseError("gpg sign failed");
  566.         }
  567.         $tar->addModify("$tmpdir/package.sig", '', $tmpdir);
  568.         return true;
  569.     }
  570.  
  571.     // }}}
  572.     // {{{ doMakeRPM()
  573.  
  574.     /*
  575.  
  576.     (cox)
  577.  
  578.     TODO:
  579.  
  580.         - Fill the rpm dependencies in the template file.
  581.  
  582.     IDEAS:
  583.  
  584.         - Instead of mapping the role to rpm vars, perhaps it's better
  585.  
  586.           to use directly the pear cmd to install the files by itself
  587.  
  588.           in %postrun so:
  589.  
  590.           pear -d php_dir=%{_libdir}/php/pear -d test_dir=.. <package>
  591.  
  592.     */
  593.  
  594.     function doMakeRPM($command, $options, $params)
  595.     {
  596.         if (sizeof($params) != 1) {
  597.             return $this->raiseError("bad parameter(s), try \"help $command\"");
  598.         }
  599.         if (!file_exists($params[0])) {
  600.             return $this->raiseError("file does not exist: $params[0]");
  601.         }
  602.         include_once "Archive/Tar.php";
  603.         include_once "PEAR/Installer.php";
  604.         include_once "System.php";
  605.         $tar = new Archive_Tar($params[0]);
  606.         $tmpdir = System::mktemp('-d pear2rpm');
  607.         $instroot = System::mktemp('-d pear2rpm');
  608.         $tmp = $this->config->get('verbose');
  609.         $this->config->set('verbose', 0);
  610.         $installer = new PEAR_Installer($this->ui);
  611.         $info = $installer->install($params[0],
  612.                                     array('installroot' => $instroot,
  613.                                           'nodeps' => true));
  614.         $pkgdir = "$info[package]-$info[version]";
  615.         $info['rpm_xml_dir'] = '/var/lib/pear';
  616.         $this->config->set('verbose', $tmp);
  617.         if (!$tar->extractList("package.xml", $tmpdir, $pkgdir)) {
  618.             return $this->raiseError("failed to extract $params[0]");
  619.         }
  620.         if (!file_exists("$tmpdir/package.xml")) {
  621.             return $this->raiseError("no package.xml found in $params[0]");
  622.         }
  623.         if (isset($options['spec-template'])) {
  624.             $spec_template = $options['spec-template'];
  625.         } else {
  626.             $spec_template = $this->config->get('data_dir') .
  627.                 '/PEAR/template.spec';
  628.         }
  629.         if (isset($options['rpm-pkgname'])) {
  630.             $rpm_pkgname_format = $options['rpm-pkgname'];
  631.         } else {
  632.             $rpm_pkgname_format = "PEAR::%s";
  633.         }
  634.  
  635.         $info['extra_headers'] = '';
  636.         $info['doc_files'] = '';
  637.         $info['files'] = '';
  638.         $info['rpm_package'] = sprintf($rpm_pkgname_format, $info['package']);
  639.         $srcfiles = 0;
  640.         foreach ($info['filelist'] as $name => $attr) {
  641.  
  642.             if ($attr['role'] == 'doc') {
  643.                 $info['doc_files'] .= " $name";
  644.  
  645.             // Map role to the rpm vars
  646.             } else {
  647.  
  648.                 $c_prefix = '%{_libdir}/php/pear';
  649.  
  650.                 switch ($attr['role']) {
  651.  
  652.                     case 'php':
  653.  
  654.                         $prefix = $c_prefix; break;
  655.  
  656.                     case 'ext':
  657.  
  658.                         $prefix = '%{_libdir}/php'; break; // XXX good place?
  659.  
  660.                     case 'src':
  661.  
  662.                         $srcfiles++;
  663.  
  664.                         $prefix = '%{_includedir}/php'; break; // XXX good place?
  665.  
  666.                     case 'test':
  667.  
  668.                         $prefix = "$c_prefix/tests/" . $info['package']; break;
  669.  
  670.                     case 'data':
  671.  
  672.                         $prefix = "$c_prefix/data/" . $info['package']; break;
  673.  
  674.                     case 'script':
  675.  
  676.                         $prefix = '%{_bindir}'; break;
  677.  
  678.                 }
  679.  
  680.                 $info['files'] .= "$prefix/$name\n";
  681.  
  682.             }
  683.         }
  684.         if ($srcfiles > 0) {
  685.             include_once "OS/Guess.php";
  686.             $os = new OS_Guess;
  687.             $arch = $os->getCpu();
  688.         } else {
  689.             $arch = 'noarch';
  690.         }
  691.         $cfg = array('master_server', 'php_dir', 'ext_dir', 'doc_dir',
  692.                      'bin_dir', 'data_dir', 'test_dir');
  693.         foreach ($cfg as $k) {
  694.             $info[$k] = $this->config->get($k);
  695.         }
  696.         $info['arch'] = $arch;
  697.         $fp = @fopen($spec_template, "r");
  698.         if (!$fp) {
  699.             return $this->raiseError("could not open RPM spec file template $spec_template: $php_errormsg");
  700.         }
  701.         $spec_contents = preg_replace('/@([a-z0-9_-]+)@/e', '$info["\1"]', fread($fp, filesize($spec_template)));
  702.         fclose($fp);
  703.         $spec_file = "$info[rpm_package]-$info[version].spec";
  704.         $wp = fopen($spec_file, "wb");
  705.         if (!$wp) {
  706.             return $this->raiseError("could not write RPM spec file $spec_file: $php_errormsg");
  707.         }
  708.         fwrite($wp, $spec_contents);
  709.         fclose($wp);
  710.         $this->ui->outputData("Wrote RPM spec file $spec_file", $command);
  711.  
  712.         return true;
  713.     }
  714.  
  715.     // }}}
  716. }
  717.  
  718. ?>
  719.